<script setup>
import { ref } from 'vue'

// 训练进度
const progress = ref({
  completed: 6,
  total: 10,
  accuracy: 85
})

// 训练课程
const courses = ref([
  {
    id: 1,
    title: '基础指令训练',
    desc: '学习基本的语音控制命令',
    completed: true,
    exercises: [
      { name: '音乐播放控制', done: true },
      { name: '音量调节', done: true },
      { name: '设备开关', done: true }
    ]
  },
  {
    id: 2,
    title: '场景指令训练',
    desc: '掌握多设备联动控制',
    completed: true,
    exercises: [
      { name: '睡眠模式', done: true },
      { name: '影音模式', done: true },
      { name: '派对模式', done: true }
    ]
  },
  {
    id: 3,
    title: '个性化训练',
    desc: '定制专属语音指令',
    completed: false,
    exercises: [
      { name: '自定义指令', done: false },
      { name: '语气识别', done: false },
      { name: '场景联动', done: false }
    ]
  }
])

// 当前训练
const currentTraining = ref({
  step: 1,
  command: '播放我喜欢的歌单',
  listening: false,
  success: null,
  attempts: 0
})

// 开始训练
const startTraining = () => {
  currentTraining.value.listening = true
  
  // 模拟语音识别
  setTimeout(() => {
    currentTraining.value.listening = false
    currentTraining.value.success = Math.random() > 0.3
    currentTraining.value.attempts++
    
    if (currentTraining.value.success) {
      // 下一步
      currentTraining.value.step++
      currentTraining.value.command = '暂停播放'
      currentTraining.value.success = null
    }
  }, 3000)
}

// 跳过训练
const skipTraining = () => {
  if (confirm('确定要跳过当前训练吗？')) {
    currentTraining.value.step++
    currentTraining.value.command = '暂停播放'
    currentTraining.value.success = null
  }
}
</script>

<template>
  <div class="training-page">
    <!-- 训练进度 -->
    <div class="progress-section">
      <div class="progress-info">
        <div class="progress-text">
          <h2>训练进度</h2>
          <div class="progress-stats">
            <span>{{ progress.completed }}/{{ progress.total }}课程</span>
            <span>准确率 {{ progress.accuracy }}%</span>
          </div>
        </div>
        <div class="progress-bar">
          <div 
            class="progress-fill"
            :style="{ width: `${(progress.completed / progress.total) * 100}%` }"
          ></div>
        </div>
      </div>
    </div>

    <!-- 训练课程 -->
    <div class="courses-section">
      <h2>训练课程</h2>
      <div class="courses-grid">
        <div 
          v-for="course in courses"
          :key="course.id"
          class="course-card"
          :class="{ completed: course.completed }"
        >
          <div class="course-header">
            <h3>{{ course.title }}</h3>
            <span 
              class="status-badge"
              :class="{ completed: course.completed }"
            >
              {{ course.completed ? '已完成' : '进行中' }}
            </span>
          </div>
          <p class="course-desc">{{ course.desc }}</p>
          <div class="exercises-list">
            <div 
              v-for="(exercise, index) in course.exercises"
              :key="index"
              class="exercise-item"
            >
              <span 
                class="check-icon"
                :class="{ done: exercise.done }"
              >
                {{ exercise.done ? '✓' : '○' }}
              </span>
              <span class="exercise-name">{{ exercise.name }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 当前训练 -->
    <div class="current-training">
      <div class="training-header">
        <h2>当前训练</h2>
        <div class="step-indicator">
          步骤 {{ currentTraining.step }}/5
        </div>
      </div>

      <div class="command-card">
        <div class="command-text">
          "{{ currentTraining.command }}"
        </div>
        <div 
          class="voice-button"
          :class="{ 
            listening: currentTraining.listening,
            success: currentTraining.success === true,
            error: currentTraining.success === false
          }"
          @click="startTraining"
        >
          <span class="mic-icon">🎙️</span>
          <span class="button-text">
            {{ 
              currentTraining.listening ? '正在聆听...' : 
              currentTraining.success === true ? '识别成功' :
              currentTraining.success === false ? '请重试' :
              '按住说话'
            }}
          </span>
        </div>
        <div class="training-actions">
          <button 
            class="skip-btn"
            @click="skipTraining"
          >
            跳过
          </button>
          <div class="attempts-count">
            尝试次数: {{ currentTraining.attempts }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.training-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

/* 进度条样式 */
.progress-section {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.progress-info {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.progress-text {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.progress-text h2 {
  margin: 0;
}

.progress-stats {
  display: flex;
  gap: 1.5rem;
  color: var(--text-gray);
}

.progress-bar {
  height: 8px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
  transition: width 0.3s ease;
}

/* 课程列表样式 */
.courses-section {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.courses-section h2 {
  margin: 0;
  margin-bottom: 2rem;
}

.courses-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
}

.course-card {
  background-color: var(--bg-dark);
  border-radius: 12px;
  padding: 1.5rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.course-card.completed {
  border-color: var(--success-color);
}

.course-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.course-header h3 {
  margin: 0;
}

.status-badge {
  padding: 0.3rem 0.8rem;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  font-size: 0.9rem;
  color: var(--text-gray);
}

.status-badge.completed {
  background-color: var(--success-color);
  color: white;
}

.course-desc {
  color: var(--text-gray);
  margin-bottom: 1.5rem;
}

.exercises-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.exercise-item {
  display: flex;
  align-items: center;
  gap: 0.8rem;
}

.check-icon {
  color: var(--text-gray);
}

.check-icon.done {
  color: var(--success-color);
}

/* 当前训练样式 */
.current-training {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.training-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.training-header h2 {
  margin: 0;
}

.step-indicator {
  color: var(--text-gray);
}

.command-card {
  background-color: var(--bg-dark);
  border-radius: 12px;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2rem;
}

.command-text {
  font-size: 1.5rem;
  font-weight: 500;
  text-align: center;
}

.voice-button {
  width: 150px;
  height: 150px;
  background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.voice-button:hover {
  transform: scale(1.05);
}

.voice-button.listening {
  animation: pulse 2s infinite;
}

.voice-button.success {
  background: var(--success-color);
}

.voice-button.error {
  background: var(--error-color);
}

.mic-icon {
  font-size: 2rem;
}

.button-text {
  font-size: 0.9rem;
}

.training-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.skip-btn {
  padding: 0.8rem 1.5rem;
  background: none;
  border: 1px solid var(--text-gray);
  color: var(--text-gray);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.skip-btn:hover {
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.attempts-count {
  color: var(--text-gray);
  font-size: 0.9rem;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(98, 0, 234, 0.4); }
  70% { box-shadow: 0 0 0 20px rgba(98, 0, 234, 0); }
  100% { box-shadow: 0 0 0 0 rgba(98, 0, 234, 0); }
}

@media (max-width: 768px) {
  .courses-grid {
    grid-template-columns: 1fr;
  }

  .voice-button {
    width: 120px;
    height: 120px;
  }

  .mic-icon {
    font-size: 1.5rem;
  }

  .command-text {
    font-size: 1.2rem;
  }
}
</style>